package com.lemon.smartsafety.ui.adapter.safe.data;

import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.DataInfo;
import com.lemon.smartsafety.entity.event.datacoll.BuildingAddCheckItemEvent;
import com.lemon.smartsafety.entity.event.datacoll.BuildingItemCountEvent;
import com.lemon.smartsafety.entity.event.datacoll.BuildingPositionRemarkEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingItemCountVo;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/27.
 */
public class BuildingPositionAdapter extends LemonRcvAdapter<CompanyBuildingItemCountVo> {
    private LoggerHelper loggerHelper = new LoggerHelper(BuildingPositionAdapter.class);

    public BuildingPositionAdapter(@Nullable List<CompanyBuildingItemCountVo> data) {
        super(data);
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new BuildingPositionItem();
    }


    public class BuildingPositionItem implements AdapterItem<CompanyBuildingItemCountVo> {


        LinearLayout rippleItemCount;
        TextView   tvItemCount;
        LinearLayout rippleAdd;
        ImageView  ivAdd;
        LinearLayout ripplePositionRemark;
        TextView   tvPositionRemark;


        CompanyBuildingItemCountVo mData;
        int                        mPosition;

        public BuildingPositionItem() {

        }

        @Override
        public int getLayoutResId() {
            return R.layout.item_position_view;
        }

        @Override
        public void bindViews(View root) {
            rippleItemCount = (LinearLayout) root.findViewById(R.id.ripple_item_count);
            tvItemCount = (TextView) root.findViewById(R.id.tv_item_count);

            rippleAdd = (LinearLayout) root.findViewById(R.id.ripple_add);
            ivAdd = (ImageView) root.findViewById(R.id.iv_add);

            ripplePositionRemark = (LinearLayout) root.findViewById(R.id.ripple_position_remark);
            tvPositionRemark = (TextView) root.findViewById(R.id.tv_position_remark);
        }

        @Override
        public void setViews() {
        }

        @Override
        public void handleData(final CompanyBuildingItemCountVo item, int position) {
            mPosition = position;
            mData = item;
            tvItemCount.setText(StringUtils.nullValue(item.getItemCount(), "0"));
            if (StringUtils.nullValue(mData.getTitleId()).length() != 0) {
                tvPositionRemark.setText(Html.fromHtml("<u>" + mData.getPositionTitle() + "(" + DataInfo.getChinesePosition(mData.getPositionMark()) + ")" + "</u>"));
            } else {
                tvPositionRemark.setText(Html.fromHtml("<u>" + DataInfo.getChinesePosition(mData.getPositionMark()) + "</u>"));
            }
            if (!"0".equals(StringUtils.nullValue(mData.getItemCount(), "0"))) {
                tvItemCount.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                rippleItemCount.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BuildingItemCountEvent event = new BuildingItemCountEvent();
                        event.setData(item);
                        AppService.getBus().post(event);
                    }
                });
            } else {
                rippleItemCount.setEnabled(false);
            }

            ripplePositionRemark.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BuildingPositionRemarkEvent event = new BuildingPositionRemarkEvent();
                    event.setData(item);
                    AppService.getBus().post(event);
                }
            });

            rippleAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BuildingAddCheckItemEvent event = new BuildingAddCheckItemEvent();
                    event.setData(item);
                    AppService.getBus().post(event);
                }
            });
        }
    }
}
